package com.talkyun.openx.server.helper;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.StringReader;
import java.util.ArrayList;
import java.util.List;

public class LineHelper {

    public static List<String> getLineList(String txt, boolean trimLine, boolean ignoreBlankLine) {
        List<String> list = new ArrayList<String>();
        BufferedReader br = new BufferedReader(new StringReader(txt));
        try {
            String line = br.readLine();
            while (line != null) {
                if (trimLine) {
                    line = line.trim();
                }
                if (!ignoreBlankLine) {
                    list.add(line);
                } else if (line.length() != 0) {
                    list.add(line);
                }
                line = br.readLine();
            }
            return list;
        } catch (IOException e) {
            throw new RuntimeException(e);
        }
    }

    public static List<String> getPartList(List<String> list, String bl, String el) {
        boolean bf = false, ef = false;
        List<String> part = new ArrayList<String>();
        for (String line : list) {
            if (line.startsWith(bl)) {
                bf = true;
                continue;
            } else if (line.startsWith(el)) {
                ef = true;
                break;
            }
            if (bf) {
                part.add(line);
            }
        }
        // not end flag
        if (!ef) {
            part.clear();
        }
        return part;
    }

    public static List<String> filter(List<String> list, LineFilter filter) {
        List<String> part = new ArrayList<String>();
        for (String line : list) {
            String result = filter.doFilter(line);
            if (result != null) {
                part.add(result);
            }
        }
        return part;
    }

    public static String getPartLine(String line, String bl, String el, boolean ignoreBlank) {
        int pos = line.indexOf(bl) + bl.length();
        int pos2 = line.indexOf(el, pos);
        String part = line.substring(pos, pos2);
        return ignoreBlank ? part.trim() : part;
    }

    public static String getLastPartLine(String line, String bl, String el, boolean ignoreBlank) {
        int pos = line.lastIndexOf(bl) + bl.length();
        int pos2 = line.indexOf(el, pos);
        String part = line.substring(pos, pos2);
        return ignoreBlank ? part.trim() : part;
    }

    public static String[] split(String line, String regx, boolean ignoreBlank) {
        String[] ss = line.split(regx);
        if (!ignoreBlank) {
            return ss;
        }
        List<String> list = new ArrayList<String>();
        for (String s : ss) {
            if (s != null && s.trim().length() > 0) {
                list.add(s.trim());
            }
        }
        return list.toArray(new String[list.size()]);
    }

    public static boolean isBlankLine(String line) {
        return line == null || line.trim().length() == 0;
    }

    public static boolean include(String line, String regx, String word, boolean ignoreBlank) {
        if (line == null) {
            return false;
        }
        String[] ss = split(line, regx, ignoreBlank);
        for (String s : ss) {
            if (s.equals(word)) {
                return true;
            }
        }
        return false;
    }

    public interface LineFilter {

        String doFilter(String line);

    }
}
